package top.hucx.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.hucx.entity.ResponseResult;
import top.hucx.entity.User;
import top.hucx.service.UserService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class Action {

    @Reference
    private UserService userService;

    @PostMapping("/register")
    public ResponseResult register(@RequestBody User user){
        user.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        Integer i = userService.register(user);
        ResponseResult result = new ResponseResult();
        result.setContent(i == 0 ? "注册失败":"注册成功");
        return result;
    }

    @PostMapping("/getUsers")
    public ResponseResult getUsers(@RequestBody User user){
        List<User> users = userService.findUserByMultiCondition(user);
        return new ResponseResult("响应成功", users);
    }

    @GetMapping("/deleteUser")
    public ResponseResult deleteUser(@Param("uid") Integer uid){
        int i = userService.deleteUser(uid);
        if (i != 0)
        return new ResponseResult("删除成功","");
        return new ResponseResult("遇到了问题","");
    }

    @GetMapping("/findUserByUid")
    public ResponseResult findUserByUid(Integer uid){
        User user = userService.findUserByUid(uid);
        return new ResponseResult("查找成功",user);
    }

    @PostMapping("/updateUser")
    public ResponseResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return new ResponseResult("修改成功","");
    }
}
